package com.xiumu.service.sys.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiumu.common.core.enums.YesNo;
import com.xiumu.common.core.exception.base.SysException;
import com.xiumu.common.core.exception.base.XiuMuException;
import com.xiumu.common.core.page.PageQuery;
import com.xiumu.common.core.utils.AssertUtil;
import com.xiumu.common.minio.MinioTemplate;
import com.xiumu.dao.sys.HaloFileDao;
import com.xiumu.pojo.sys.entity.HaloFile;
import com.xiumu.pojo.sys.query.HaloFileQuery;
import com.xiumu.service.sys.HaloFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 文件上传表 Service 接口实现
 *
 * @author XiuMu
 * @Date 2023-01-29 11:49:11
 */
@Service
public class HaloFileServiceImpl extends ServiceImpl<HaloFileDao, HaloFile> implements HaloFileService {

    @Autowired
    private MinioTemplate minioTemplate;

    @Override
    public Page<HaloFile> listPage(PageQuery<HaloFileQuery, HaloFile> pageQuery) {
        return this.baseMapper.selectPage(pageQuery.toPage(), pageQuery.getCondition());
    }

    @Override
    public List<HaloFile> listByFileQuery(HaloFileQuery file) {
        return this.baseMapper.selectByFileQuery(file);
    }

    @Override
    public String uploadFile(MultipartFile file) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            log.error("文件上传 FileServiceImpl#uploadFile.getInputStream 发生错误", e);
            throw new XiuMuException(SysException.FILE_ERROR);
        }
        String originalFilename = file.getOriginalFilename();
        String key = minioTemplate.uploadFile(inputStream, file.getSize(), originalFilename);
        HaloFile uploadHaloFile = new HaloFile()
                .setFileKey(key)
                .setFilePath(minioTemplate.getPublicUrlOfDefaultBucketFile(key))
                .setFileName(originalFilename)
                .setFileSize(file.getSize())
                .setFileType(file.getContentType())
                .setFileSuffix(FileUtil.getSuffix(originalFilename));
        this.save(uploadHaloFile);
        return uploadHaloFile.getFilePath();
    }

    @Override
    public Boolean deleteByFileKey(String fileKey) {
        if (StrUtil.isNotBlank(fileKey)){
            HaloFile haloFile = new HaloFile();
            haloFile.setDeleteFlag(YesNo.YES);
            this.update(haloFile, new LambdaUpdateWrapper<HaloFile>().eq(HaloFile::getFileKey, fileKey));
        }
        return true;
    }

    @Override
    public Boolean removeFileById(Long id) {
        HaloFile haloFile = getById(id);
        AssertUtil.isNotNull(haloFile, SysException.FILE_NOT_EXIST);
        minioTemplate.deleteFileOfDefaultBucketFile(haloFile.getFileKey());
        return this.removeById(id);
    }
}




